//
//  TLGestureRecognizerDelegate.m
//  KidReading
//
//  Created by telen on 14/12/15.
//  Copyright (c) 2014年 刘赞黄Telen. All rights reserved.
//

#import "TLGestureRecognizerDelegate.h"

@interface UIView (TLGestureRecognizerDelegate)
@end
@implementation UIView (TLGestureRecognizerDelegate)
- (void)transformToscale11
{
    self.transform = CGAffineTransformMakeScale(1, 1);
}
@end

@implementation TLTapImageView
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.userInteractionEnabled = YES;
    }
    return self;
}

- (void)dealloc
{
    self.normalImage = nil;
    self.selectImage = nil;
}

@end

@implementation TLGestureRecognizerDelegate

static TLGestureRecognizerDelegate* tlGestureRecognizerDelegateSingleTapView = nil;
static TLGestureRecognizerDelegate* tlGestureRecognizerDelegateSingleTapTLTapImageView = nil;
static TLGestureRecognizerDelegate* tlGestureRecognizerDelegateSingleTapView_undo = nil;
static NSDate* tlLastTapDate = nil;

+ (TLGestureRecognizerDelegate *)shareSingleTapView
{
    if (!tlGestureRecognizerDelegateSingleTapView) {
        tlGestureRecognizerDelegateSingleTapView = [[TLGestureRecognizerDelegate alloc] init];
    }
    return tlGestureRecognizerDelegateSingleTapView;
}

+ (TLGestureRecognizerDelegate *)shareSingleTapTLTapImageView
{
    if (!tlGestureRecognizerDelegateSingleTapTLTapImageView) {
        tlGestureRecognizerDelegateSingleTapTLTapImageView = [[TLGestureRecognizerDelegate alloc] init];
    }
    return tlGestureRecognizerDelegateSingleTapTLTapImageView;
}

+ (TLGestureRecognizerDelegate *)shareSingleTapView_Undo
{
    if (!tlGestureRecognizerDelegateSingleTapView_undo) {
        tlGestureRecognizerDelegateSingleTapView_undo = [[TLGestureRecognizerDelegate alloc] init];
    }
    return tlGestureRecognizerDelegateSingleTapView_undo;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if (tlLastTapDate){
        NSTimeInterval itv = [[NSDate date] timeIntervalSinceDate:tlLastTapDate];
        if (itv<0) {
            itv = -(itv);
        }
        if (itv < __tapInterVal) {
            return NO;
        }
    }
    tlLastTapDate = [NSDate date];
    if (gestureRecognizer.delegate == tlGestureRecognizerDelegateSingleTapView) {
        UIView* view = gestureRecognizer.view;
        [UIView animateWithDuration:0.1 animations:^{
            view.transform = CGAffineTransformMakeScale(1.05, 1.05);
        }completion:^(BOOL finished) {
            [view performSelector:@selector(transformToscale11) withObject:nil afterDelay:1];
        }];
    }
    
    if (gestureRecognizer.delegate == tlGestureRecognizerDelegateSingleTapTLTapImageView) {
        UIView* view = gestureRecognizer.view;
        if ([view isKindOfClass:[TLTapImageView class]]) {
            TLTapImageView* tlview = (TLTapImageView*)view;
            tlview.image = tlview.selectImage;
            [tlview setNeedsLayout];
        }
    }
    //防止向superView传递
    UIView* view = gestureRecognizer.view;
    for(UIView *v in view.subviews){
        for (UIGestureRecognizer* gest in v.gestureRecognizers) {
            if ([gest isKindOfClass:[UITapGestureRecognizer class]]) {
                CGPoint touchLocation = [touch locationInView:v];
                if (CGRectContainsPoint(v.bounds, touchLocation)){
                    return NO;
                }
            }
        }
    }
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer;
{
//    NSLog(@"%@",gestureRecognizer);
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded
        || gestureRecognizer.state == UIGestureRecognizerStateFailed) {
        if (gestureRecognizer.delegate == tlGestureRecognizerDelegateSingleTapView) {
            UIView* view = gestureRecognizer.view;
            view.transform = CGAffineTransformMakeScale(1, 1);
            [NSObject cancelPreviousPerformRequestsWithTarget:view selector:@selector(transformToscale11) object:nil];
        }
        
        if (gestureRecognizer.delegate == tlGestureRecognizerDelegateSingleTapTLTapImageView) {
            UIView* view = gestureRecognizer.view;
            if ([view isKindOfClass:[TLTapImageView class]]) {
                TLTapImageView* tlview = (TLTapImageView*)view;
                tlview.image = tlview.normalImage;
                [tlview setNeedsLayout];
            }
        }
    }
    return NO;//都返回no 互斥tap
}

@end
